package org.szgb.console.sys.role.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;
import org.szgb.console.utils.PageDto;
import org.szgb.core.base.entity.BeanEntity;

import java.io.Serial;
import java.time.LocalDateTime;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class SysRole extends BeanEntity<SysRole> {
    @Serial
    private static final long serialVersionUID = 1L;
    private String name;

    public SysRole() {
    }

    public SysRole(String id) {
        this.setId(id);
    }

    public SysRole(SysRole.SysRoleFindDto sysRoleFindDto) {
        this.setId(sysRoleFindDto.getId());
        this.setName(sysRoleFindDto.getName());
        this.setPageDto(sysRoleFindDto.getPageDto());
    }

    public SysRole(SysRole.SysRoleSaveDto sysRoleSaveDto) {
        this.setId(sysRoleSaveDto.id);
        this.setName(sysRoleSaveDto.name);
    }

    public SysRole.SysRoleView toSysRoleView() {
        return new SysRole.SysRoleView(this);
    }

    @Data
    @Schema(title = "角色信息", description = "角色信息")
    public static class SysRoleView {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "创建时间")
        private LocalDateTime createdAt;
        @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
        @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
        @Schema(description = "更新时间")
        private LocalDateTime updatedAt;
        @Schema(description = "创建人id")
        private String createdById;
        @Schema(description = "创建人姓名")
        private String createdByName;
        @Schema(description = "更新人id")
        private String updatedById;
        @Schema(description = "更新人姓名")
        private String updatedByName;

        public SysRoleView(SysRole sysRole) {
            this.id = sysRole.getId();
            this.name = sysRole.getName();
            this.createdAt = sysRole.getCreatedAt();
            this.updatedAt = sysRole.getUpdatedAt();
            if (sysRole.getCreatedBy() != null) {
                this.createdById = sysRole.getCreatedBy().getId();
                this.createdByName = sysRole.getCreatedBy().getName();
            }
            if (sysRole.getUpdatedBy() != null) {
                this.updatedById = sysRole.getUpdatedBy().getId();
                this.updatedByName = sysRole.getUpdatedBy().getName();
            }
        }

        public static List<SysRole.SysRoleView> getViewListFromList(List<SysRole> sysRoleList) {
            return sysRoleList.stream().map(SysRole.SysRoleView::new).toList();
        }
    }

    @Data
    @Schema(title = "角色查询表单", description = "角色查询参数")
    public static class SysRoleFindDto {
        @Schema(description = "ID")
        private String id;
        @Schema(description = "名称")
        private String name;
        @Schema(description = "分页信息")
        private PageDto pageDto;

        public SysRole toSysRole() {
            return new SysRole(this);
        }
    }

    @Data
    @Schema(title = "角色保存表单", description = "角色保存参数")
    public static class SysRoleSaveDto {
        @Schema(description = "id(如为空则为新增数据)")
        private String id;
        @Schema(description = "名称")
        private String name;

        public SysRole toSysRole() {
            return new SysRole(this);
        }
    }
}

